package cn.wolfcode.contorller;

import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    
    //查询全部列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQuery query){
        Page<Travel> page = travelService.query(query);
        model.addAttribute("page",page);
        return "travel/list";
    }
    //跳转添加与编辑页面
    @RequestMapping("/input")
    public  String input(Model model,String id){
        //编辑回显
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            model.addAttribute("travel",travel);
        }
        
      
        /*添加直接跳转*/
        return "travel/input";
    }
    //添加与编辑的方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){
        //保存与编辑保存方法
        travelService.saveOrUpdate(travel);
        //返回一个json
        return JsonResult.success();
    }
    



    
    //删除方法
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        travelService.delete(id);
        return JsonResult.success();
    }

    //更改发布状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id , int state){
        travelService.changeState(id,state);
        return JsonResult.success();
    }
    
    //查看游记
    
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id ){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel.getContent());
    }
}
